﻿using System;
using Internal.Runtime.Services.Battle;
using UnityEngine;

namespace Runtime.Services.Battle {

    public abstract partial class DFR_BattleServiceBase<TRecord> : DFR_BattleServiceBase {
    }

    public abstract class DFR_BattleServiceBase {

        protected sealed class EntityNoAssetVehicle : MonoBehaviour, IDFR_BattleEntityNoAssetVehicle, IDisposable, IDFR_BattleEntityAssetVehicleInternal {

            private void Awake() {
                hideFlags = HideFlags.HideInInspector;
            }

            public event IDFR_BattleEntityAssetVehicle.LoadedDelegate Loaded {
                add => throw new System.InvalidProgramException();
                remove => throw new System.InvalidProgramException();
            }

            public object Content => throw new System.InvalidProgramException();

            void IDisposable.Dispose() { }

            void IDFR_BattleEntityAssetVehicleInternal.OnLoaded(object @object) { }
        }

        protected abstract class EntityAssetVehicle<T> : EntityAssetVehicle, IDFR_BattleEntityAssetVehicle<T> where T : class {

            protected new T Content => base.Content as T;

            T IDFR_BattleEntityAssetVehicle<T>.Content => Content;
        }

        protected abstract class EntityAssetVehicle : MonoBehaviour, IDFR_BattleEntityAssetVehicle, IDFR_BattleEntityAssetVehicleInternal, IDisposable {

            private void Awake() {
                hideFlags = HideFlags.HideInInspector;
            }

            public event IDFR_BattleEntityAssetVehicle.LoadedDelegate Loaded;

            protected object Content;

            object IDFR_BattleEntityAssetVehicle.Content => Content;

            protected abstract object GetContent(object @object);

            public void OnLoaded(object @object) {
                Content = GetContent(@object);
                if (Content != null) {
                    Loaded?.Invoke();
                }
            }

            void IDisposable.Dispose() => OnDestroy();

            private void OnDestroy() {
                Content = null;
                Loaded = null;
            }
        }
    }
}
